package com.cq.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/7/1 11:37
 * @Description: ${description}
 * @Version: 1.0
 */
@Configuration
public class ShiroConfig {

    @Bean
    public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator=new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setUsePrefix(true);
        return defaultAdvisorAutoProxyCreator;
    }

    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        bean.setLoginUrl("/login");
        bean.setSuccessUrl("/");
        bean.setUnauthorizedUrl("/401");
        //添加shiro的内置过滤器
        /**
         * anon:无需认证就可以访问
         * authc:必须认证才能访问
         * 必须拥有 记住我 功能才能访问
         * perms: 拥有某个资源的的权限才能访问文档
         * role: 拥有某个角色权限才能访问到
         */
        //拦截
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/login", "anon");
        filterMap.put("/logout", "logout");
        filterMap.put("/getCode", "anon");
        filterMap.put("/wxGetOpenId", "anon");
        filterMap.put("/webrtc/user", "anon");
        filterMap.put("/meeting/updateMeetingAndRoom", "anon");
        filterMap.put("/user/sendMessage", "anon");
        filterMap.put("/send", "anon");
        filterMap.put("/meeting/sendMeetingFailMessage", "anon");
        filterMap.put("/admin/loginByPassword", "anon");
        filterMap.put("/webSocket", "anon");
        filterMap.put("/sendCode", "anon");

        //授权,正常情况下，没有授权会跳到未授权页面

        //未授权页面

        //拦截
        filterMap.put("/", "authc");
        filterMap.put("/index", "authc");
        filterMap.put("/user/list","authc");
        filterMap.put("/user/role","authc");
        filterMap.put("/user/admin","authc");
        filterMap.put("/user/info","authc");
        filterMap.put("/user/sign","authc");
        filterMap.put("/updateAdminPassword","authc");
        filterMap.put("/addAdminUI","authc");
        filterMap.put("/addRoleUI","authc");
        filterMap.put("/bannerList","authc");
        filterMap.put("/addBannerUI","authc");
        filterMap.put("/userAuth","authc");
        filterMap.put("/meetingList","authc");
        filterMap.put("/rtmp","authc");
        filterMap.put("/webrtcList","authc");
        filterMap.put("/recommendMeeting","authc");
        filterMap.put("/topicList","authc");
        filterMap.put("/recommendTopic","authc");
        filterMap.put("/replyList","authc");
        filterMap.put("/fileManagement","authc");

        //注入
        bean.setFilterChainDefinitionMap(filterMap);

        //设置登录的请求
        bean.setLoginUrl("/login");


        return bean;
    }

    //DefaultWebSecurityManager
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);

        return securityManager;
    }
    //创建 realm 对象,需要自定义
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
